package zhoug.demo.chart2.chart;

import android.graphics.Color;
import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.SeekBar;
import android.widget.TextView;

import zhoug.chart.charts.MPHorizontalBar;
import zhoug.chart.utils.MPChartUtils;
import zhoug.demo.chart2.BaseActivity;
import zhoug.demo.chart2.R;
import zhoug.common.utils.ViewUtils;

import androidx.appcompat.widget.SwitchCompat;

/**
 * 自定义水平进度条
 *
 * @Author: zhoug
 * @Date: 2024-07-30
 * @Description:
 */
public class MPHorizontalBarActivity extends BaseActivity {
    private static final int DURING = 1000;
    private static final int startColor = Color.parseColor("#854751");
    private static final int endColor = MPChartUtils.setColorAlphaPercent(0.2f, startColor);
    private SwitchCompat mSwitchWithMode;
    private SwitchCompat mSwitchArc;
    private SwitchCompat mSwitchGradientColor;
    private SeekBar mSeekBar;
    private MPHorizontalBar mHorizontalBar;
    private TextView mTvValue;
    private LinearLayout mRoot;

    @Override
    protected int getLayoutResId() {
        return R.layout.activity_mp_horizontal_bar;
    }

    @Override
    protected void findViews() {

        mSwitchWithMode = findViewById(R.id.switch_withMode);
        mSwitchArc = findViewById(R.id.switch_arc);
        mSwitchGradientColor = findViewById(R.id.switch_gradientColor);
        mSeekBar = findViewById(R.id.seekBar);
        mHorizontalBar = findViewById(R.id.horizontalBar);
        mTvValue = findViewById(R.id.tv_value);
        mRoot = findViewById(R.id.root);
    }


    @Override
    protected void init() {
        mSwitchWithMode.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                int widthMode = isChecked ? MPHorizontalBar.WIDTH_MODE_AUTO : MPHorizontalBar.WIDTH_MODE_FIXED;

                ViewUtils.findView(mRoot, MPHorizontalBar.class, true, horizontalBar -> {
                    horizontalBar.setWidthMode(widthMode);
                    horizontalBar.requestLayout();
                });
            }
        });

        mSwitchArc.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                mHorizontalBar.setTopArc(isChecked);
                mHorizontalBar.notifyData();
            }
        });

        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                mHorizontalBar.setProgress(progress);
                mHorizontalBar.notifyData();
                mTvValue.setText(String.valueOf(progress));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
                mHorizontalBar.startAnim(DURING);
            }
        });
        mSwitchGradientColor.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {

                if (isChecked) {
                    mHorizontalBar.setGradientColor(startColor, endColor);
                } else {
                    mHorizontalBar.setGradientColorNull();
                }
                mHorizontalBar.notifyData();
            }
        });
        mHorizontalBar.setProgress(50);
        mTvValue.setText("50");
        mSeekBar.setProgress(50);
        mHorizontalBar.setForeColor(startColor);
        mHorizontalBar.startAnim(DURING);

    }


}
